const fs = require("fs");
const path = require("path");


class EditConfig {

    constructor() {
        // 定义配置文件路径
        this.configFilePath = path.join(__dirname, "configuration.json");

        this.proxyIP = '';
        this.proxyPort = 0;
        this.scanFolder = [];
    }

    readConfig() {
        const configData = fs.readFileSync(this.configFilePath, "utf8");
        const config = JSON.parse(configData);
        this.proxyIP = config.proxyIP;
        this.proxyPort = config.proxyPort;
        this.scanFolder = config.scanFolder;
        return config;
    }

    writeConfig() {
        fs.writeFile(this.configFilePath, JSON.stringify(this.getConfig(), null, 2), (err) => {
            if (err) {
                console.error("保存配置时出错：", err);
                throw err;
            }
            console.log('配置保存成功')
        });
    }


    setProxy(proxy) {
        this.readConfig();
        this.proxyIP = proxy.proxyIP;
        this.proxyPort = proxy.proxyPort;
        this.writeConfig();
    }

    setScanFolder(folder) {
        this.readConfig();
        this.scanFolder.push(folder);
        this.writeConfig();
    }

    removeScanFolder(index) {
        this.readConfig();
        this.scanFolder.splice(index, 1);
        this.writeConfig();
    }

    getConfig() {
        return {
            proxyIP: this.proxyIP,
            proxyPort: this.proxyPort,
            scanFolder: this.scanFolder
        }
    }

}

module.exports = EditConfig; // 导出DLsiteScraper类
